package com.card323.crpg.battle;

import java.util.ArrayList;

import com.card323.crpg.hero.HeroAPListener;

public class ActionPoint {
	public volatile int _color_less;
	public volatile int _black;
	public volatile int _green;
	public volatile int _purple;
	public volatile int _blue;
	public volatile int _red;

	ArrayList<HeroAPListener> _hero_ap_listener_list = new ArrayList<HeroAPListener>();

	public void add_hero_ap_listener_list(HeroAPListener listener) {
		_hero_ap_listener_list.add(listener);
	}

	public ActionPoint() {
		_color_less = 0;
		_black = 0;
		_green = 0;
		_purple = 0;
		_blue = 0;
		_red = 0;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		sb.append("无:" + _color_less);
		sb.append("|");
		sb.append("黑:" + _black);
		sb.append("|");
		sb.append("绿:" + _green);
		sb.append("|");
		sb.append("紫:" + _purple);
		sb.append("|");
		sb.append("蓝:" + _blue);
		sb.append("|");
		sb.append("红:" + _red);
		sb.append(")");
		return sb.toString();
	}

	public ActionPoint(int color_less, int black, int green, int purple,
			int blue, int red) {
		_color_less = color_less;
		_black = black;
		_green = green;
		_purple = purple;
		_blue = blue;
		_red = red;
	}

	public ActionPoint clone() {
		ActionPoint ap = new ActionPoint();
		ap.set_action_point(this);
		return ap;
	}

	public synchronized void minus_action_point(ActionPoint minus_ap) {
		_color_less -= minus_ap._color_less;
		_black -= minus_ap._black;
		_green -= minus_ap._green;
		_purple -= minus_ap._purple;
		_blue -= minus_ap._blue;
		_red -= minus_ap._red;
		if (_color_less < 0)
			_color_less = 0;
		if (_black < 0)
			_black = 0;
		if (_green < 0)
			_green = 0;
		if (_purple < 0)
			_purple = 0;
		if (_blue < 0)
			_blue = 0;
		if (_red < 0)
			_red = 0;
		ap_changed(null,this);
	}

	public synchronized void add_action_point(ActionPoint add_ap) {
		_color_less += add_ap._color_less;
		_black += add_ap._black;
		_green += add_ap._green;
		_purple += add_ap._purple;
		_blue += add_ap._blue;
		_red += add_ap._red;
		ap_changed(null,this);
	}

	public synchronized void set_action_point(int color_less, int black,
			int green, int purple, int blue, int red) {
		_color_less = color_less;
		_black = black;
		_green = green;
		_purple = purple;
		_blue = blue;
		_red = red;
		ap_changed(null,this);
	}

	public synchronized void set_action_point(ActionPoint ap) {
		_color_less = ap._color_less;
		_black = ap._black;
		_green = ap._green;
		_purple = ap._purple;
		_blue = ap._blue;
		_red = ap._red;
		ap_changed(null,this);
	}

	public synchronized boolean is_bigger_than(ActionPoint cost) {
		if (_color_less < cost._color_less)
			return false;
		if (_black < cost._black)
			return false;
		if (_green < cost._green)
			return false;
		if (_purple < cost._purple)
			return false;
		if (_blue < cost._blue)
			return false;
		if (_red < cost._red)
			return false;
		return true;

	}

	public void clear() {
		_color_less = 0;
		_black = 0;
		_green = 0;
		_purple = 0;
		_blue = 0;
		_red = 0;
		ap_changed(null,this);
	}

	public void ap_changed(ActionPoint old, ActionPoint current) {
		for(HeroAPListener listener :_hero_ap_listener_list)
		{
			listener.on_ap_changed(old, current);
		}

	}
}
